import 'package:flutter/material.dart';
//import 'package:english';

//void main() => runApp(MyApp());

void main() => runApp(MyApp());
//class MyApp extends StatelessWidget {
//  // This widget is the root of your application.
//  @override
//  Widget build(BuildContext context) {
//    return MaterialApp(
//      title: 'Flutter Demo',
//      theme: ThemeData(
//        // This is the theme of your application.
//        //
//        // Try running your application with "flutter run". You'll see the
//        // application has a blue toolbar. Then, without quitting the app, try
//        // changing the primarySwatch below to Colors.green and then invoke
//        // "hot reload" (press "r" in the console where you ran "flutter run",
//        // or simply save your changes to "hot reload" in a Flutter IDE).
//        // Notice that the counter didn't reset back to zero; the application
//        // is not restarted.
//        primarySwatch: Colors.blue,
//      ),
//      home: MyHomePage(title: 'Flutter Demo Home Page'),
//    );
//  }
//}

int Func() {
  return 22;
}

//void showDesc({var name, var age}) {
//  if (name != null) {
//    print('name = $name');
//  }
//  if (age != null) {
//    print('age = $age');
//  }
//}

//void showDesc(var name, [var age = 18]) {
//  if (name != null) {
//    print('name = $name');
//  }
//  if (age != null) {
//    print('age = $age');
//  }
//}

// 函数定义
//void println(String name) {
//  print("name = $name");
//}
//
//void showDesc(var name, Function log) {
//  log(name);
//}

//void println(String name) {
//  print('name = $name');
//}

void showDesc(String name) {
  //函数中定义函数
  void println(var name) {
    print('name = $name');
  }

  println(name);
}

class Person {
  var name;
  var age;

  Person(this.name, this.age);
}

/*函数和类代码定义*/
getFruit() => Banana(20,'黄色'); //获取一个水果对象

class Fruit {
  Fruit() {
    print('这是不带参数的Fruit构造函数');
  }

  Fruit.desc(var desc) {
    print('$desc in Fruit');
  }

  Fruit.weight(var weight) {
    //
  }

  void showName(var name) {
    print('name = $name');
  }
}

class LogUtil {
  void log() {
    print('this is a log');
  }
}

class Banana {

  var weight;
  var color;

  Banana(this.weight, this.color);

  void showWeight() {
    print('weight = $weight');
  }

  void showColor() {
    print('color = $color');
  }
}

class Apple extends Fruit with LogUtil {
  Apple():super() {
    print('这是不带参数的Apple构造函数'
        '构造函数');
  }

  var color;

  Apple.desc(var desc) {
    print('$desc in Apple');
  }
}

class FruitFactory<T> {
  T produceFruit(T t) {
    return t;
  }
}

class Tree {
  var desc;

  //命名构造函数
  Tree.init() {
    desc = 'this is a seed';
  }

  //函数运行之前初始化实例变量
  Tree(var des) : desc = des;
}




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    //可变
    dynamic a;
    a = '哈哈';
    a = 88;
    print('value = $a');

    var b;
    b = 999;
    b = 'sss';
    b = 12;
    print('b = $b');

    String c = '😆';
    print('c = $c');

    const age = 18;
    print(age);

    const int m1 = 1;
    final int m2 = Func();
    print('m1 = $m1, m2 = $m2');

    int d = 12;

    num f = 1.1111;
    print('sss $f');

    String sr1 = 'hello ';
    String sr2 = '''jack
    This is XiaoMing.
    ''';
    print(sr1 + sr2);

    bool isMe = true;
    print(isMe);

    List arr = ['Bruce', 'Nick', 'John'];
    arr.add('sss');
    arr.remove('sss');
    arr.removeAt(0);
    arr.insert(0, '插入');
    print('arr = $arr');

    Map map1 = {'name': '小明', 'age': 18, 'high': 1.72};

    print('map1 = $map1, name = ${map1['name']}');

    var map2 = {1: 'hi', 2: 'hello', 3: 'yep'};
    print('map2 = $map2');

    // 函数调用
    showDesc("Bruce");

    final number1 = 1;
    final number2 = 2;

    if (number1 == null) {
      print('哈哈');
    }

    Person p = new Person("", "0");
    var name = p?.name;
    print('person\'s name = $name');

    var num1 = 10;
    var result = num1 ~/ 3; //得出一个小于等于(num/3)的最大整数
    print('$result, ${1 + 2}');

    dynamic bb = Banana(20, '黄色');
    print('b.weight = ${(bb as Banana).weight}');

    var b1 = getFruit();
    if (b is Apple) {
      print('The fruit is an Apple');
    } else if (b1 is Banana) {
      print('The fruit is a banana');
    }

    String name1;
    String nickName = name1 ?? 'Nick'; //如果name不为null，则nickName值为name的值，否则值为Nick
    print('nickName = $nickName');


    Banana(10, '黄色')
      ..showWeight()
      ..showColor();

    var strings = ['1', '2', '3'];
    for (var item in strings) {
      print(item);
      if (item == '1') {
        break;
      }
    }

    switch (1) {
      case 1:
        print('sss');
      break;
    }

    void handleOperator() => throw Exception('this operator exception');
    try {
      handleOperator();
    } on Exception catch (e) {
      print(e);
    } finally {
      print('finally');
    }

    Tree t = Tree.init();
    print('${t.desc}');

    Tree t1 = Tree('this is a tree');
    print('${t1.desc}');

    var aa = Apple();
    Apple.desc('say hello');
    aa.showName('苹果');
    aa.log();


    FruitFactory<Apple> fruit  = FruitFactory<Apple>();
    Apple a1 = fruit.produceFruit(Apple());
    a1.showName('泛型');

    return new MaterialApp(
      title: '😁',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('hello'),
          backgroundColor: Colors.deepOrange,
          centerTitle: true,
          actions: <Widget>[
            //创建按钮
            new IconButton(
                icon: new Icon(Icons.add_call),
                tooltip: 'add Alarm',
                onPressed: () {
                  print('哈哈哈哈');
                }),
            //菜单栏
            new PopupMenuButton(
              itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
                    PopupMenuItem<String>(
                      child: Text('热度'),
                      value: 'hot',
                    ),
                    PopupMenuItem<String>(
                      child: Text('最新'),
                      value: 'new',
                    )
                  ],
              onSelected: (String action) {
                switch (action) {
                  case 'hot':
                    print(action);
                    break;
                  case 'new':
                    print(action);
                    break;
                }
              },
            ),
          ],
        ),
        body: new Center(
          child: new Text('welcome to Flutter'),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
